/** 
 * This file is part of osami-fin-demo project( http://code.google.com/p/osami-fin-demo/ )
 * @license Apache License 2.0
 */
package org.osami.appevent;

import java.util.Dictionary;
import java.util.Hashtable;

import org.osgi.service.event.Event;
import org.osgi.service.event.EventAdmin;


/** 'typedef' for adding call event properties */
class CallEventProperty extends Hashtable<String, String> {
	private static final long serialVersionUID = 0x1L;
}

/**
 * This is given for Android BroadcastReceivers to allow sending OSGi events and
 * implements the EventAdmin event creation for the phone events.
 * */
public class EventDispatcher {

	public static final String MESSAGE_TYPE_SMS = "org/osami/phone/sms";
	public static final String MESSAGE_TYPE_CALL_INCOMING = "org/osami/phone/callIncoming";
	public static final String MESSAGE_TYPE_CALL_OUTGOING = "org/osami/phone/callOutgoing";

	public static final String CALL_STATE_IDLE = "CALL_STATE_IDLE";
	public static final String CALL_STATE_RINGING = "CALL_STATE_RINGING";
	public static final String CALL_STATE_OFFHOOK = "CALL_STATE_OFFHOOK";

	public static final String PROPERTY_CALL_STATE = "CALLSTATE";
	public static final String PROPERTY_CALLER_NAME = "CALLER_NAME";
	public static final String PROPERTY_CALLER_NUMBER = "CALLER_NUMBER";
	
	public static final String SMS_CONTENTS = "CONTENTS";
	public static final String SMS_SENDER_NAME = "SENDER_NAME";
	public static final String SMS_SENDER_NUMBER = "SENDER_PHONENUMBER";

	protected Logger log;
	protected EventAdmin eventAdmin;

	public EventDispatcher(EventAdmin aEventAdmin) {
		this.eventAdmin = aEventAdmin;
		this.log = new Logger(this.getClass().getName());
	}

	protected void createMessage(String aMsgType,
			Dictionary<String, String> aProperties) {
		log.d("Posting event via EventAdmin:", aMsgType);
		eventAdmin.postEvent(new Event(aMsgType, aProperties));
	}

	public void makeEventCallIncoming(String aCallState, String aCallerNumber,
			String aCallerName) {
		log.d("Creating incoming call event:", aCallState, aCallerNumber);

		CallEventProperty props = new CallEventProperty();
		props.put(PROPERTY_CALL_STATE, aCallState);
		props.put(PROPERTY_CALLER_NAME, aCallerName);
		props.put(PROPERTY_CALLER_NUMBER, aCallerNumber);
		this.createMessage(MESSAGE_TYPE_CALL_INCOMING, props);
	}

	public void makeEventCallOutgoing(String aCallState, String aCallerNumber,
			String aCallerName) {
		CallEventProperty props = new CallEventProperty();
		props.put(PROPERTY_CALL_STATE, aCallState);
		props.put(PROPERTY_CALLER_NAME, aCallerName);
		props.put(PROPERTY_CALLER_NUMBER, aCallerNumber);
		this.createMessage(MESSAGE_TYPE_CALL_OUTGOING, props);
	}

	public void makeEventSMS(String msg, String phoneNumber, String sender) {
		CallEventProperty props = new CallEventProperty();
		props.put(SMS_CONTENTS, msg);
		props.put(SMS_SENDER_NAME, sender);
		props.put(SMS_SENDER_NUMBER, phoneNumber);
		// TODO: Caller\called to?
		this.createMessage(MESSAGE_TYPE_SMS, props);
	}
}

